package com.queclink.ycm.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.parser.HexUtil;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.common.utils.HttpURLUtils;
import lombok.Data;

import java.util.List;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-21 16:31:00
 */
@Data
public class Report extends FrameType {

    private String TimeType;

    private String FrameLength;

    private String MultiPacketFlag;

    private String FrameCount;

    private String FrameNumber;

    private String IMEI;

    private String DeviceType;

    private String ProtocolVersion;

    private String CustomVersion;

    private String ExtendedZone;

    private String CountNumber;

    private byte CheckByte;

    private List<Record> Records;

    @JSONField(
            name = "TimeType"
    )
    public String getTimeType() {
        return this.TimeType;
    }

    public void setTimeType(String timeType) {
        this.TimeType = timeType;
    }



    public void setCustomVersion(String customVersion) {
        this.CustomVersion = customVersion;
    }

    @JSONField(
            serialize = false
    )
    public String getFrameLength() {
        return this.FrameLength;
    }

    public void setFrameLength(String frameLength) {
        this.FrameLength = frameLength;
    }

    @JSONField(
            name = "MultiPacketFlag"
    )
    public String getMultiPacketFlag() {
        return "00";
    }

    public void setMultiPacketFlag(String multiPacketFlag) {
        this.MultiPacketFlag = multiPacketFlag;
    }

    @JSONField(
            name = "FrameCount"
    )
    public String getFrameCount() {
        return this.FrameCount;
    }

    public void setFrameCount(String frameCount) {
        this.FrameCount = frameCount;
    }

    @JSONField(
            name = "FrameNumber"
    )
    public String getFrameNumber() {
        return this.FrameNumber;
    }




    public void setProtocolVersion(String protocolVersion) {
        this.ProtocolVersion = protocolVersion;
    }

    @JSONField(
            name = "ExtendedZone"
    )
    public String getExtendedZone() {
        return "00";
    }

    public void setExtendedZone(String extendedZone) {
        this.ExtendedZone = extendedZone;
    }

    @JSONField(
            name = "CountNumber"
    )
    public String getCountNumber(JSONObject jsonObject) {
        Integer countNumber = jsonObject.getInteger("CountNumber");
        String countNumberStr = HexUtils.intToHex(countNumber, 4);
        return countNumberStr;
    }

    public void setCountNumber(String countNumber) {
        this.CountNumber = countNumber;
    }

    @JSONField(
            name = "CheckByte",
            serialize = false
    )
    public byte getCheckByte() {
        return this.CheckByte;
    }

    public void setCheckByte(byte checkByte) {
        this.CheckByte = checkByte;
    }

    @JSONField(
            name = "Records"
    )
    public List<Record> getRecords() {
        return this.Records;
    }

    public void setRecords(List<Record> records) {
        this.Records = records;
    }

    public Report() {
        this.MessageType = "Report";
    }

    public void parser() {
    }

    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append("2B00");
        sb.append(this.getMultiPacketFlag());
        Header header = new Header();
        JSONObject headJsonObject = jsonObject.getJSONObject("Head");
        sb.append(header.getIMEI(headJsonObject));
        sb.append(header.getDeviceType(headJsonObject));
        sb.append(header.getProtocolVersion(headJsonObject));
        sb.append(header.getCustomVersion(headJsonObject));
//        sb.append(this.getExtendedZone());
        sb.append("00");

        JSONObject bodyJsonObject = jsonObject.getJSONObject("Body");
        JSONArray records = bodyJsonObject.getJSONArray("Records");
        for (Object record : records) {
            JSONObject recordJsonObject = JSONObject.parseObject(JSONObject.toJSONString(record));
            Record rec = new Record();
            String result = rec.combine(recordJsonObject);
            sb.append(result);
        }

        JSONObject tailJsonObject = jsonObject.getJSONObject("Tail");
        sb.append(this.getCountNumber(tailJsonObject));

        int len = sb.length() + 8;
        len = len / 2;

        sb.insert(4, HexUtils.intToHex(len, 4));

//        String input = "";
//        String regex = "(.{2})";
//        input = sb.toString().replaceAll(regex, "$1+");
//        input = input.substring(0, input.length() - 1);


        byte[] buf = HexUtils.hexStr2bytes(sb.toString());
        byte checksum = HexUtil.crc8_calc(buf, buf.length);

//        StringBuffer param = new StringBuffer();
//        param.append("data=");
//        param.append(input);
//        param.append("&width=8&poly=31&init=FF&xor=00&refin=false&refout=false");
//        String s = HttpURLUtils.doPost2("http://api.ip33.com/ss/crc/c", param.toString());
//        JSONObject crcJsonObject = JSONObject.parseObject(s);
//        sb.append(crcJsonObject.getString("hex"));
        sb.append(String.format("%02X", checksum));
        sb.append("24");
        return sb.toString().toUpperCase();
    }


}
